package com.lngsyz.eseal.base.biz.service;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.lngsyz.eseal.base.entity.LoginInfo;
import com.lngsyz.eseal.base.entity.biz.AppInfoForAdmin;
import com.lngsyz.eseal.base.entity.biz.AppInfoForNormal;
import com.lngsyz.eseal.base.entity.biz.FileInfo;
import com.lngsyz.eseal.base.entity.biz.GetCurrentInfo;
import com.lngsyz.eseal.base.entity.biz.InAddApp;
import com.lngsyz.eseal.base.entity.biz.InAddAppFile;
import com.lngsyz.eseal.base.entity.biz.InApplyReview;
import com.lngsyz.eseal.base.entity.biz.InGetRefuseReason;
import com.lngsyz.eseal.base.entity.biz.InGetTicket;
import com.lngsyz.eseal.base.entity.biz.InOpen;
import com.lngsyz.eseal.base.entity.biz.InReviewAppInfo;
import com.lngsyz.eseal.base.entity.biz.InUserInfo;
import com.lngsyz.eseal.base.entity.biz.OutGetAppFile;
import com.lngsyz.eseal.base.entity.biz.OutGetRefuseReason;
import com.lngsyz.eseal.base.entity.biz.OutGetTicket;
import com.lngsyz.eseal.base.entity.biz.OutUserInfo;
import com.lngsyz.eseal.base.entity.source1.TApp;
import com.lngsyz.eseal.base.entity.source1.TFile;
import com.lngsyz.eseal.base.entity.source1.TUser;
import com.lngsyz.eseal.base.exception.RequestSystemException;
import com.lngsyz.eseal.base.util.CommonUtil;
import com.lngsyz.eseal.base.util.jwt.TokenUtil;

@Service
public class BizService {

	@Autowired
	TUserService tUserService;
	@Autowired
	TAppService tAppService;
	@Autowired
	RedisService redisService;
	@Autowired
	TFileService tFileService;
	
	/**
	 * 获取当前用户信息
	 * @param loginInfo
	 * @return
	 */
	public GetCurrentInfo getCurrentInfo(LoginInfo loginInfo) {
		if(loginInfo != null) {
			GetCurrentInfo gci = new GetCurrentInfo();
			gci.setId(loginInfo.getId());
			gci.setAccount(loginInfo.getMobile());
			gci.setAdmin(loginInfo.getAdmin());
			gci.setGmtCreate(loginInfo.getGmtCreate());
			return gci;
		} else {
			throw new RequestSystemException("无法查询到当前账号信息");
		}
	}

	/**
	 * 根据申请列表获取授权信息
	 * @param inGetTicket
	 * @param loginInfo
	 * @return
	 */
	public OutGetTicket getTicket(InGetTicket inGetTicket, LoginInfo loginInfo) throws Exception {
		return tAppService.getTicket(inGetTicket, loginInfo);
	}

	/**
	 * 【普通用户】提交申请
	 * @param addApp
	 * @param loginInfo
	 */
	public void addApp(InAddApp inAddApp, LoginInfo loginInfo) throws Exception {
		int count = tAppService.findReviewingCount(loginInfo.getId());
		if(count < 2) {
			TApp tApp = new TApp();
			tApp.setId(CommonUtil.createUuid());
			tApp.setSysName(inAddApp.getSysName());
			tApp.setSysRemark(inAddApp.getSysRemark());
			tApp.setSysUrl(inAddApp.getSysUrl());
			tApp.setApplyName(inAddApp.getApplyName());
			tApp.setApplyDept(inAddApp.getApplyDept());
			tApp.setApplyDeptRemark(inAddApp.getApplyDeptRemark());
			TFile tFile1 = tFileService.addFile(inAddApp.getFile1());
			TFile tFile2 = tFileService.addFile(inAddApp.getFile2());
			tApp.setFile1(tFile1.getId());
			tApp.setFile2(tFile2.getId());
			tAppService.addApp(tApp, loginInfo);
		} else {
			throw new RequestSystemException("当前用户存在" + count + "个申请，正处在审批中，请优先处理完成后再提交。");
		}
	}

	/**
	 * 【普通用户】对接申请列表
	 * @param loginInfo
	 * @return
	 */
	public List<AppInfoForNormal> getAppInfoForNormal(LoginInfo loginInfo, int page, int size) throws Exception {
		TApp tApp = new TApp();
		tApp.setUserCreate(loginInfo.getId());
		List<TApp> appList = tAppService.findList(tApp, page, size);
		return tApp2AppInfoForNormal(appList);
	}
	
	private List<AppInfoForNormal> tApp2AppInfoForNormal(List<TApp> tApps){
		List<AppInfoForNormal> rtn = new ArrayList<AppInfoForNormal>();
		for(TApp tApp : tApps) {
			AppInfoForNormal aifn = new AppInfoForNormal();
			aifn.setId(tApp.getId());
			aifn.setSysName(tApp.getSysName());
			aifn.setSysRemark(tApp.getSysRemark());
			aifn.setSysUrl(tApp.getSysUrl());
			aifn.setApplyName(tApp.getApplyName());
			aifn.setApplyDept(tApp.getApplyDept());
			aifn.setApplyDeptRemark(tApp.getApplyDeptRemark());
			aifn.setGmtCreate(CommonUtil.dateToStrLong(tApp.getGmtCreate()));
			aifn.setApplyPassFlag(tApp.getApplyPassFlag());
			aifn.setOpenFlag(tApp.getOpenFlag());
			rtn.add(aifn);
		}
		return rtn;
	}
	
	/**
	 * 【普通用户】对接申请列表---数量
	 * @param loginInfo
	 * @return
	 */
	public int getAppCountForNormal(LoginInfo loginInfo) throws Exception {
		TApp tApp = new TApp();
		tApp.setUserCreate(loginInfo.getId());
		int count = tAppService.findListCount(tApp);
		return count;
	}

	/**
	 * 【普通用户】补全材料（上传文件资质）
	 * @param inAddAppFile
	 */
	public void addAppFile(InAddAppFile inAddAppFile, LoginInfo loginInfo) throws Exception {
		tAppService.addFiles(inAddAppFile, loginInfo);
	}

	/**
	 * 【管理员】待审核列表
	 * @param inReviewAppInfo
	 * @param loginInfo
	 * @return
	 */
	public List<AppInfoForAdmin> getAppInfoForAdmin(InReviewAppInfo inReviewAppInfo, LoginInfo loginInfo, int page, int size) throws Exception {
		List<TApp> appList = new ArrayList<TApp>();
		if(StringUtils.equals(loginInfo.getAdmin(), "1")) {
			TApp tApp = new TApp();
			tApp.setMobile(inReviewAppInfo.getAccount());
			tApp.setSysName(inReviewAppInfo.getSysName());
			tApp.setApplyName(inReviewAppInfo.getApplyName());
			tApp.setApplyDept(inReviewAppInfo.getApplyDept());
			tApp.setApplyPassFlag(inReviewAppInfo.getApplyPassFlag());
			tApp.setOpenFlag(inReviewAppInfo.getOpenFlag());
			tApp.setUserCreate(inReviewAppInfo.getUserCreate());
			appList = tAppService.findList(tApp, page, size);
		}
		return tApp2AppInfoForAdmin(appList);
	}
	
	private List<AppInfoForAdmin> tApp2AppInfoForAdmin(List<TApp> tApps){
		List<AppInfoForAdmin> rtn = new ArrayList<AppInfoForAdmin>();
		for(TApp tApp : tApps) {
			AppInfoForAdmin aifn = new AppInfoForAdmin();
			aifn.setId(tApp.getId());
			aifn.setSysName(tApp.getSysName());
			aifn.setSysRemark(tApp.getSysRemark());
			aifn.setSysUrl(tApp.getSysUrl());
			aifn.setApplyName(tApp.getApplyName());
			aifn.setApplyDept(tApp.getApplyDept());
			aifn.setApplyDeptRemark(tApp.getApplyDeptRemark());
			aifn.setGmtCreate(CommonUtil.dateToStrLong(tApp.getGmtCreate()));
			aifn.setApplyPassFlag(tApp.getApplyPassFlag());
			aifn.setOpenFlag(tApp.getOpenFlag());
			rtn.add(aifn);
		}
		return rtn;
	}

	/**
	 * 【管理员】对接申请列表---数量
	 * @param loginInfo
	 * @return
	 */
	public int getAppCountForAdmin(InReviewAppInfo inReviewAppInfo, LoginInfo loginInfo) throws Exception {
		int count = 0;
		if(StringUtils.equals(loginInfo.getAdmin(), "1")) {
			TApp tApp = new TApp();
			tApp.setMobile(inReviewAppInfo.getAccount());
			tApp.setSysName(inReviewAppInfo.getSysName());
			tApp.setApplyName(inReviewAppInfo.getApplyName());
			tApp.setApplyDept(inReviewAppInfo.getApplyDept());
			tApp.setApplyPassFlag(inReviewAppInfo.getApplyPassFlag());
			tApp.setOpenFlag(inReviewAppInfo.getOpenFlag());
			tApp.setUserCreate(inReviewAppInfo.getUserCreate());
			count = tAppService.findListCount(tApp);
		}
		return count;
	}

	public OutGetAppFile getAppFile(String id, LoginInfo loginInfo) throws Exception {
		OutGetAppFile rtn = new OutGetAppFile();
		TApp tApp = tAppService.findOne(id);
		if(tApp != null) {
			String file1Id = tApp.getFile1();
			rtn.setFile1(packageFileInfo(file1Id));
			String file2Id = tApp.getFile2();
			rtn.setFile2(packageFileInfo(file2Id));
		}
		return rtn;
	}
	
	private FileInfo packageFileInfo(String fileId) throws Exception {
		FileInfo fi = null;
		if(StringUtils.isNotBlank(fileId)) {
			TFile tFile = tFileService.getFile(fileId);
			if(tFile != null) {
				fi = new FileInfo();
				fi.setName(tFile.getNetName());
				fi.setBase64(CommonUtil.localFile2Base64(tFile.getLocalPath() + "/" + tFile.getLocalName()));
			}
		}
		return fi;
	}
	
	/**
	 * 【管理员】用户列表
	 * @param inUserInfo
	 * @param loginInfo
	 * @return
	 */
	public List<OutUserInfo> getUserInfo(InUserInfo inUserInfo, LoginInfo loginInfo, int page, int size) throws Exception {
		List<TUser> userList = new ArrayList<TUser>();
		if(StringUtils.equals(loginInfo.getAdmin(), "1")) {
			userList = tUserService.findList(inUserInfo, page, size);
		}
		return tUser2OutUserInfo(userList);
	}
	
	private List<OutUserInfo> tUser2OutUserInfo(List<TUser> userList){
		List<OutUserInfo> rtn = new ArrayList<OutUserInfo>();
		for(TUser tUser : userList) {
			OutUserInfo ui = new OutUserInfo();
			ui.setId(tUser.getId());
			ui.setAccount(tUser.getMobile());
			ui.setDistrict(tUser.getDistrictCity());
			ui.setAdmin(tUser.getAdmin());
			ui.setGmtCreate(CommonUtil.dateToStrLong(tUser.getGmtCreate()));
			rtn.add(ui);
		}
		return rtn;
	}
	
	/**
	 * 【管理员】用户列表---数量
	 * @param loginInfo
	 * @return
	 */
	public int getUserInfoCount(InUserInfo inUserInfo, LoginInfo loginInfo) throws Exception {
		int count = 0;
		if(StringUtils.equals(loginInfo.getAdmin(), "1")) {
			count = tUserService.findListCount(inUserInfo);
		}
		return count;
	}

	/**
	 * 【管理员】审核通过/不通过
	 * @param inApplyReview
	 * @param loginInfo
	 */
	public void reviewApp(InApplyReview inApplyReview, LoginInfo loginInfo) throws Exception {
		tAppService.applyPassReview(inApplyReview, loginInfo);
	}

	/**
	 * 【管理员】启用/停用
	 * @param inOpen
	 * @param loginInfo
	 */
	public void open(InOpen inOpen, LoginInfo loginInfo) throws Exception {
		tAppService.open(inOpen, loginInfo);
	}

	/**
	 * 【普通用户】查看被驳回原因
	 * @param inGetRefuseReason
	 * @return
	 * @throws Exception
	 */
	public OutGetRefuseReason getRefuseReason(InGetRefuseReason inGetRefuseReason, LoginInfo loginInfo) throws Exception {
		return tAppService.getRefuseReason(inGetRefuseReason);
	}

	/**
	 * 退出
	 * @param loginInfo
	 */
	public void logout(LoginInfo loginInfo) {
		tUserService.logout(loginInfo);
	}

	/**
	 * 刷新token
	 * @param loginInfo
	 * @return
	 */
	public String refreshToken(LoginInfo loginInfo) {
		boolean b = redisService.getTemp(loginInfo.getId());
		if(b) {
			// create jwt token
			String token = TokenUtil.generateToken(loginInfo);
			return token;
		} else {
			throw new RequestSystemException("当前用户长时间未操作，更新失败");
		}
	}
}
